package org.restoran.service.impl;

import com.google.common.base.Preconditions;
import org.restoran.model.*;
import org.restoran.repository.ItemRepository;
import org.restoran.service.ItemService;
import org.restoran.service.parser.Parser;
import org.restoran.service.parser.dto.JsonItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Writer;
import java.util.Collection;
import java.util.Objects;

import static com.google.common.base.Preconditions.*;

/**
 * Created with IntelliJ IDEA.
 * User: Andrew Kononenko
 * Date: 13.11.13
 * Time: 9:43
 * To change this template use File | Settings | File Templates.
 */
@Repository
@Service ("itemService")
@Transactional (propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public class ItemServiceImpl implements ItemService {

    private static Logger lg = LoggerFactory.getLogger(OrderServiceImpl.class);


    @Autowired
    public ItemServiceImpl(Parser parser, ItemRepository itemRepository) {
        this.parser = parser;
        this.itemRepository = itemRepository;
    }

    private final Parser parser;

    private final ItemRepository itemRepository;

    @Override
    public Item findOne(Integer integer) {
        return itemRepository.findOne(integer);
    }

    @Override
    public Iterable<Item> findAll() {
        return itemRepository.findAll();
    }

    @Override
    public Item save(Item entity)
    {
        checkNotNull(entity);
        checkArgument(entity.isNew(), "Entity must be new");

        return itemRepository.save(entity);
    }

    @Override
    public Item updateOne(Item item)
    {
        checkNotNull(item);
        checkArgument(!item.isNew(), "Entity must not be new");

        return updateItem(item);
    }

    private Item updateItem(Item entity)
    {

        lg.debug("update newEntity from entity {}", entity);

        Item itemToUpdate = itemRepository.findOne(entity.getId());

        itemToUpdate.setCategory(entity.getCategory());
        itemToUpdate.setDescription(entity.getDescription());
        itemToUpdate.setName(entity.getName());
        itemToUpdate.setPrice(entity.getPrice());
 
        return itemRepository.save(itemToUpdate);
	}

	@Override
    public void delete(Item deleted) {
        itemRepository.delete(deleted);
    }

    @Override
    public void delete(Integer deletedId) {
        itemRepository.delete(deletedId);
    }

    @Override
    public String parseToJson(Item entity) {
        return parser.toJson(entity);
    }

    @Override
    public void parseToJson(Writer writer, Item entity) {
        parser.toJson(writer, entity);
    }

    @Override
    public String parseToJson(Collection<Item> objects) {
        return parser.toJson(objects);
    }

    @Override
    public void parseToJson(Writer writer, Collection<Item> args) {

    }

    @Override
    public Item parseFromJson(String json, Class<Item> type) {
        JsonItem itm = parser.fromJson(json, JsonItem.class);

        Item res = new Item();
        res.setId(itm.getId());
        res.setCategory(new Category(itm.getCategoryId()));
        res.setPrice(itm.getPrice());
        res.setDescription(itm.getDescription());
        res.setName(itm.getName());

        return res;
    }


}

